package com.ebt.m.customer.view;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import com.ebt.m.R;
import com.ebt.m.customer.adapter.AdapterCommonFragmentPager;
import com.ebt.m.customer.constant.CustomerConstant;
import com.ebt.m.customer.entity.CustomerDetail;
import com.ebt.m.customer.ui.FragmentCustomerAttachment;
import com.ebt.m.customer.ui.FragmentCustomerInteraction;
import com.ebt.m.customer.ui.FragmentCustomerNote;
import com.ebt.m.customer.ui.FragmentCustomerPolicy;
import com.ebt.m.customer.ui.FragmentCustomerProfile;
import com.ebt.m.customer.ui.FragmentCustomerPropose;
import com.ebt.m.customer.util.DisplayUtil;
import com.ebt.m.widget.SlidingTabLayout;

import java.util.ArrayList;

/**
 * 客户详情底部view
 * Created by Rick.Wang on 2016/6/12.
 */
public class CustomerAllContentView extends FrameLayout {

    public static final String VIEW_PAGER_INDEX = "VIEW_PAGER_INDEX";

    private CustomerDetail mCustomerDetail;
    private String mCustomerUuid;
    private String mCustomerName;
    private String mCustomerMode;
    private int mViewPagerIndex;

    private SlidingTabLayout mSlidingTabLayout;
    private ViewPager mViewPager;
    private AdapterCommonFragmentPager mAdapter;

    private FragmentCustomerProfile mFragmentCustomerProfile; // 保持对象，用于从基本信息页面获取CustomerDetail

    public CustomerAllContentView(Context context) {
        this(context, null);
    }

    public CustomerAllContentView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflate(context, R.layout.view_customer_all_content, this);
    }

    public void update(CustomerDetail customerDetail, String customerMode, String customerUuid, int viewPagerIndex) {
        storeArgs(customerDetail, customerMode, customerUuid, viewPagerIndex);
        initMainContent();
    }

    private void storeArgs(CustomerDetail customerDetail, String customerMode, String customerUuid, int viewPagerIndex) {
        mCustomerDetail = customerDetail;
        mCustomerName = mCustomerDetail.getName();
        mCustomerUuid = customerUuid;
        mCustomerMode = customerMode;
        mViewPagerIndex = viewPagerIndex;
    }

    private void initMainContent() {
        findViews();
        initAdapter();
        initViewPager();
        initSlidingTab();
        switchViewPager();
    }

    private void findViews() {
        mViewPager = (ViewPager) findViewById(R.id.fragment_customer_all_view_pager);
        mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tab_layout);

    }

    private void initAdapter() {
        mAdapter = new AdapterCommonFragmentPager(
                ((FragmentActivity) getContext()).getSupportFragmentManager(),
                getTitles(),
                getFragments()
        );
    }

    private void initViewPager() {
        mViewPager.setOffscreenPageLimit(6);
        mViewPager.setAdapter(mAdapter);
    }

    private void initSlidingTab() {
        mSlidingTabLayout.setDistributeEvenly(true);
        mSlidingTabLayout.setDistributeEvenlyWhenWidthNotFull(true, DisplayUtil.getScreenWidth(getContext()));
        mSlidingTabLayout.setViewPager(mViewPager);
    }

    private void switchViewPager() {
        switch (mViewPagerIndex) {
            case CustomerConstant.CustomerAll.VIEW_PAGER_INDEX_PROFILE:
                mViewPager.setCurrentItem(0, false);
                break;
            case CustomerConstant.CustomerAll.VIEW_PAGER_INDEX_INTERACTION:
                mViewPager.setCurrentItem(1, false);
                break;
            case CustomerConstant.CustomerAll.VIEW_PAGER_INDEX_INSURANCE:
                mViewPager.setCurrentItem(2, false);
                break;
            case CustomerConstant.CustomerAll.VIEW_PAGER_INDEX_PROPOSAL:
                mViewPager.setCurrentItem(3, false);
                break;
            case CustomerConstant.CustomerAll.VIEW_PAGER_INDEX_NOTE:
                mViewPager.setCurrentItem(4, false);
                break;
            case CustomerConstant.CustomerAll.VIEW_PAGER_INDEX_ATTACHMENT:
                mViewPager.setCurrentItem(5, false);
                break;
        }
    }

    private ArrayList<String> getTitles() {
        ArrayList<String> titles = new ArrayList<String>();
        titles.add("基本信息");
        titles.add("保单");
        titles.add("计划书");
        titles.add("笔记");
        titles.add("附件");
        titles.add("动态");
        return titles;
    }

    private ArrayList<Fragment> getFragments() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        //基本信息
        mFragmentCustomerProfile = FragmentCustomerProfile.newInstance(mCustomerDetail, mCustomerMode, mCustomerUuid, true, false);
        fragments.add(mFragmentCustomerProfile);
        //保单
        fragments.add(FragmentCustomerPolicy.newInstance(mCustomerUuid));
        //计划书
        fragments.add(FragmentCustomerPropose.newInstance(mCustomerUuid, mCustomerName));
        //笔记
        fragments.add(FragmentCustomerNote.newInstance(mCustomerUuid));
        //附件
        fragments.add(FragmentCustomerAttachment.newInstance(mCustomerUuid));
        //动态
        fragments.add(FragmentCustomerInteraction.newInstance(mCustomerUuid));
        return fragments;
    }

    public CustomerDetail getCustomerDetail() {
        return mFragmentCustomerProfile == null ? null : mFragmentCustomerProfile.getCustomerDetail();
    }
}